import router from "./router/index";
import { useUserStore } from "./store/modules/user";
import pinia from "./store/index";

router.beforeEach(async (to: any, from: any, next: any) => {
  document.title = `收银系统 - ${to.meta.title}`;
  const userStore = useUserStore(pinia);
  const username = userStore.userInfo.username;
  if (username) {
    if (to.path == "/login") {
      next();
    } else {
      if (username) {
        next();
      } else {
        next({ path: "/login", query: { redirect: to.path } });
      }
    }
  } else {
    // 用户未登录判断
    if (to.path == "/login") {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});

// 全局后置守卫
// router.afterEach((to: any, from: any) => {
//   console.log("==全局后置守卫==>", to, from);
// });
